package fgoScript.entity.guda;

import commons.entity.DD;
import commons.util.GameUtil;
import commons.util.PropertiesUtil;
import destinychild.entity.PointAndColor;
import fgoScript.constant.GameConstant;
import fgoScript.constant.PointInfo;
import fgoScript.entity.*;
import fgoScript.exception.AppNeedStopException;
import fgoScript.service.CommonMethods;
import fgoScript.service.EventFactors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AllGudazi extends AbstractApGudazi{
    private static final Logger LOGGER = LogManager.getLogger(EventGudazi.class);

    @Override
    public void intoAndSelect(int apNum, int account) throws Exception {

    }

    @Override
    public void fightAndStop(boolean rebootFlag, int apNum, int account) throws Exception {
        fightAndBackForEvent(rebootFlag);

    }

    @Override
    public Point getSuppotServant() {
        return PointInfo.getInstance().getpServantMix();
    }

    @Override
    public void fightOverMethod() {

    }
    private int getMaxIndex(int[] array){
        int max = array[0]; // 假设第一个元素是最大值
        int index = 0; // 最大值的下标

        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i]; // 更新最大值
                index = i; // 更新下标
            }
        }
        return index;
    }
    private void fightAndBackForEvent(boolean rebootFlag) throws Exception {

        // 获取战斗策略实例；
        String sTName =PropertiesUtil.getValueFromStrategyFile("STName");
        BStrategy bs = BStrategy.getTrainStrategyByName(sTName);
        List<BServant> bServantList = bs.getbServants();
        List<Point> clothSkillPoints = bs.getClothSkillPoints();
        List<Point> changePersonPoints = bs.getChangePersonPoints();

        // 1回合数字高亮点
        List<BRound> bRoundList = bs.getbRounds();
        List<Point> fiveCardPoints = bs.getFiveCardPoints();
        int[][][] servantSkills = bs.getServantSkills();
        int[][][] afterClothSkills = bs.getAfterClothSkills();
        int[][][] clotheSkills = bs.getClotheSkills();
        int[][] monsterSelects = bs.getMonsterSelects();
        int[][] npGos = bs.getNpGos();
        String[][] cardSequence = bs.getCardSequence();

        int eveValue;
        int battleRounds = 0;
        // 获取最大战斗次数；
        int MaxRounds = 20;
        int maxTurns = servantSkills.length;
        int skillSize;
        int cSkillSize;
        BServant fromServant;
        int fromSkillIndex;
        int toPersonIndex;
        PointColor pc;
        int rBcount;
        for (int i = 0; i < maxTurns; i++) {
            rBcount = 0;
            LOGGER.info("战斗等待");
            CommonMethods.waitToAttack(null, LOGGER);
            LOGGER.info("第"+ (i+1+"") +"回合开始");
            // 获取回合标识点集合；
            List<Point> pointList = bRoundList.get(i).getPhases();
            eveValue = GameUtil.getColorEveValue(pointList);
            while (eveValue > GameConstant.THRESHOLD && battleRounds < MaxRounds) {

                //前置判断是否到最后一回合
//                if(GameUtil.isSTOP_SCRIPT()){
//                    throw new AppNeedStopException();
//                }
                pc = CommonMethods.waitToAttack(null, LOGGER);
                if ("attack".equals(pc.getName())) {
                }else {
                    LOGGER.info("羁绊点，跳出战斗！");
                    break;
                }
                if(rBcount++==0){
                    // 选择怪物
                    List<Point> targetList = bRoundList.get(i).getEnemys();
                    if(monsterSelects[i].length != 0){
                        if(monsterSelects[i][0] != 0){
                            GameUtil.getMymouse().mouseMoveByPoint(targetList.get(monsterSelects[i][0]-1),true,true);
                            GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                        }
                    }


                    //执行从者技能
                    int[][] skills = servantSkills[i];
                    skillSize= skills.length;
                    for (int j = 0; j < skillSize; j++) {
                        // 等待
//                        if(GameUtil.isSTOP_SCRIPT()){
//                            throw new AppNeedStopException();
//                        }
                        CommonMethods.waitToAttack(null, LOGGER);

                        fromServant = bServantList.get(skills[j][0]-1);
                        fromSkillIndex = skills[j][1]-1;
                        toPersonIndex = skills[j][2]-1;
                        GameUtil.getMymouse().mouseMoveByPoint(fromServant.getSkLocs().get(fromSkillIndex),true,true);


                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
                        // 选择人物
                        GameUtil.getMymouse().mouseMoveByPoint(bServantList.get(toPersonIndex).getSetLoc(),true,true);

                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
                        GameUtil.delay(1000);
                    }
                    //执行战斗服技能
                    int[][] cRSkills = clotheSkills[i];
                    int  cRSkillSize = cRSkills.length;

                    for (int j = 0; j < cRSkillSize; j++) {
                        // 等待
                        CommonMethods.waitToAttack(null, LOGGER);
                        //执行战斗服技能
                        fromSkillIndex = cRSkills[j][0]-1;
                        toPersonIndex = cRSkills[j][1]-1;
                        // 选择衣牌
                        GameUtil.getMymouse().mouseMoveByPoint(clothSkillPoints.get(0),true,true);

                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                        // 选择衣牌技能
                        GameUtil.getMymouse().mouseMoveByPoint(clothSkillPoints.get(fromSkillIndex+1),true,true);

                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);


                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                        if( cRSkills[j].length > 2 ){
                            // 选择交换人物1
                            GameUtil.getMymouse().mouseMoveByPoint(changePersonPoints.get(cRSkills[j][1]-1),true,true);

                            GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

                            GameUtil.delay(1000);

                            // 选择交换人物2
                            if(cRSkills[j][2]!=6){
                                GameUtil.getMymouse().mouseMoveByPoint(changePersonPoints.get(cRSkills[j][2]-1),true,true);

                                GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

                                GameUtil.delay(1000);
                            }


                            // 确认
                            GameUtil.getMymouse().mouseMoveByPoint(changePersonPoints.get(6),true,true);

                            GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

                        }else{
                            // 选择人物
                            GameUtil.getMymouse().mouseMoveByPoint(bServantList.get(toPersonIndex).getSetLoc(),true,true);

                            GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                        }

                    }
                    //执行后置技能
                    skills = afterClothSkills[i];
                    skillSize= skills.length;
                    for (int j = 0; j < skillSize; j++) {
                        // 等待
                        CommonMethods.waitToAttack(null, LOGGER);

                        fromServant = bServantList.get(skills[j][0]-1);
                        fromSkillIndex = skills[j][1]-1;
                        toPersonIndex = skills[j][2]-1;
                        GameUtil.getMymouse().mouseMoveByPoint(fromServant.getSkLocs().get(fromSkillIndex),true,true);


                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
                        // 选择人物
                        GameUtil.getMymouse().mouseMoveByPoint(bServantList.get(toPersonIndex).getSetLoc(),true,true);

                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
                        GameUtil.delay(1000);
                    }
                }

                // 等待
                CommonMethods.waitToAttack(null, LOGGER);
                // 蓝色圆板选择
                CommonMethods.blueAttackSelect();
                // 点击宝具
                int npSize;
                npSize = npGos[i].length;
                for (int k = 0; k < npSize; k++) {
                    GameUtil.getMymouse().mouseMoveByPoint(bServantList.get(npGos[i][k]-1).getBjLoc(),true,true);

                    GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                }
                // 点击5卡片
                int cardSize = fiveCardPoints.size();
                List<PointColor> Alls = new ArrayList<>();;
                List<PointColor> Reds = new ArrayList<>();;
                List<PointColor> Greens = new ArrayList<>();;
                List<PointColor> Blues = new ArrayList<>();;
                Color tempColor;
                PointColor tempPc;
                int[] numbers = new int[3];
                int index = 0;
                Point p =fiveCardPoints.get(0);
                // 分组颜色
                for (int j = 0; j < cardSize; j++) {

                    tempColor = GameUtil.getScreenPixel(new Point((int)p.getX()+254*j,(int)p.getY()));
                    tempPc = new PointColor();
                    tempPc.setColor(tempColor);
                    tempPc.setPoint(fiveCardPoints.get(j));

                    numbers[0] = tempColor.getRed();
                    numbers[1] = tempColor.getGreen();
                    numbers[2] = tempColor.getBlue();
                    index = getMaxIndex(numbers);
                    if(0==index){
                        Reds.add(tempPc);
                    }else if(1==index){
                        Greens.add(tempPc);
                    }else{
                        Blues.add(tempPc);
                    }
                }
                int size = cardSequence.length;
                for (int j = 0; j < size; j++) {
                    if("R".equals(cardSequence[i][j]) && Reds.size() != 0){
                        GameUtil.getMymouse().mouseMoveByPoint(Reds.get(0).getPoint(),true,true);
                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                        Reds.remove(0);
                    }else if("G".equals(cardSequence[i][j]) && Greens.size() != 0){
                        GameUtil.getMymouse().mouseMoveByPoint(Greens.get(0).getPoint(),true,true);
                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                        Greens.remove(0);
                    }else if("B".equals(cardSequence[i][j]) && Blues.size() != 0){
                        GameUtil.getMymouse().mouseMoveByPoint(Blues.get(0).getPoint(),true,true);
                        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                        Blues.remove(0);
                    }
                }
                Alls.addAll(Reds);
                Alls.addAll(Greens);
                Alls.addAll(Blues);
                size = Alls.size();
                for (int j = 0; j < size; j++) {
                    GameUtil.getMymouse().mouseMoveByPoint(Alls.get(j).getPoint(),true,true);
                    GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
                }
                // 等待
                battleRounds++;
                CommonMethods.waitToAttack(i+"", LOGGER);
                GameUtil.delay(1000);
                eveValue = GameUtil.getColorEveValue(pointList);

            }
        }
    }

    public static void main(String[] args) {
        DD.INSTANCE.DD_mov(200,200);
    }
}
